<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use DB;

class Goods extends Model
{
    protected $table = 'goods';
    public $timestamps = false;
    protected $guarded = [];
    
    /**
     * 获取商品(商品回收站)列表
     * @param integer $goods_status 1:正常商品|-1:回收站列表
     * @return type
     */
    public static function getGoodsList($goods_status=1 , $goods_name='')
    {
        $data = self::where('goods_status', $goods_status)
                ->where('goods_name', 'like', '%'.$goods_name.'%')
                ->orderBy('goods_addtime', 'desc')
                ->select(['id','cate_id','goods_name','goods_thumb','goods_price','goods_salenum','is_sale'])
                ->paginate(15);
        $category = DB::table('category');
        foreach($data as $v){
            $v->cate_name = $category->where('id', $v->cate_id)->pluck('cate_name');
        }
        return $data;
    }
    
    /**
     * 执行插入操作
     * @param type $data
     * @return boolean
     */
    public function add($data){
        // 接收商品的值
        $goodsData = [];
        $goodsData['goods_name'] = $data['goods_name'];
        $goodsData['cate_id'] = $data['cate_id'];
        $goodsData['goods_price'] = $data['goods_price'];
        $goodsData['goods_brief'] = $data['goods_brief'];
        $goodsData['goods_img'] = $data['goods_img'];
        $goodsData['goods_thumb'] = $data['goods_thumb'];
        $goodsData['goods_desc'] = $data['goods_desc'];
        $goodsData['goods_addtime'] = date('Y-m-d H:i:s');
        
        // 插入商品信息到数据库
        $goods_id = $this->insertGetId($goodsData);
        
        if(false !== $goods_id){
            // 接收属性的值
            $attrData = [];
            foreach($data['attr_type'] as $k=>$v){
                $attrData[] = [
                    'goods_id' => $goods_id,
                    'attr_type' => $v,
                    'attr_price' => $data['attr_price'][$k],
                ];
            }
            
            // 执行插入属性表
            $res = DB::table('goods_attr')->insert($attrData);
            if($res){
                return true;
            }
        }
        
        return false;
    }
    
    // 获取当前分类及子分类下所有的商品列表
    public static function GoodsList($cate_id = 0, $page=15, $data=array())
    {
        $s_k = isset($data['sk']) ? $data['sk'] : 'goods_saletime';
        $s_v = isset($data['sv']) ? $data['sv'] :  'desc';
        $goods_name = isset($data['q']) ? $data['q'] : '';
        
        if($cate_id == 0){
            $goodsList = self::where('goods_status', '1')
                    ->where('goods_name', 'like', '%' . $goods_name . '%')
                    ->where('is_sale', '1')
                    ->orderBy($s_k, $s_v)
                    ->paginate($page);
        } else {
            // 获取该分类及子分类的商品
            // 先获取当前分类的所有子分类id
            $ids = Category::getSonsId($cate_id);
            $ids = explode(',', $ids);
            $goodsList = self::where('goods_status', '1')
                        ->where('goods_name', 'like', '%' . $goods_name . '%')
                        ->where('is_sale', '1')
                        ->whereIn('cate_id', $ids)
                        ->orderBy($s_k, $s_v)
                        ->paginate($page);
        }
        
        // 带上商品属性
        foreach($goodsList  as $goods) {
            $goods->attr = DB::table('goods_attr')->where('goods_id', $goods->id)->first();
        }
        
        return $goodsList;
    }
}